var Player,Mrow,Mcol,TahCount,PushCount;
var Game = nivel[0];

Object.extend(Number.prototype, {
	toSrc: function (){ return 'img/P' + this.toString() + '.gif';}
});
function elemov (element,r0,c0) {
	var r,c,id,ele;
	if(element.tagName.toLowerCase() != 'img') {return null;}
	if(! element.hasAttribute('r')) {return null;}
	if(! element.hasAttribute('c')) { return null; }
	r = Number(element.readAttribute('r'));
	c = Number(element.readAttribute('c'));
	r += r0;
	c += c0;
	if(r < 0 || r > Mrow || c < 0 || c > Mcol) {return null;}
	var id = 'R' + (r).toString()+ 'C' + c.toString();
	ele = $(id);
	return ele ? ele : null;

}
function eleup(element) {
	return element.elemov(-1,0);
}
function eledown(element) {
	return element.elemov(1,0);
}
function eleleft(element) {
	return element.elemov(0,-1);
}
function eleright(element) {
	return element.elemov(0,1);
}
function can(element,smer) {
	var up = element[smer]();
	if (up === null)    { return 0; }
	if (up.stav == '9') { return 0; }
	if ((/[03]/).test(up.stav)) { return 1; }
	var upup = up[smer]();
	return (/[03]/).test(upup.stav) ? 2 : 0;

}
function canup(element) {
	return element.can('eleup');
}
function candown(element) {
	return element.can('eledown');
}
function canleft(element) {
	return element.can('eleleft');
}
function canright(element) {
	return element.can('eleright');
}

Element.addMethods({
	elemov: elemov,
	eleup: eleup,
	eledown: eledown,
	eleleft: eleleft,
	eleright: eleright,
	can: can,
	canup: canup,
	candown: candown,
	canleft: canleft,
	canright: canright
});

function start() {
    Game = nivel[Number($('nivelo').value) - 1];
    $('jinydiv').value = '';
    $('sokob').update();
    zacatek();
}

function parse_game(game) {
	var prevod = {' ': 0,'#': 9,'@': 5,'$': 4,'.': 3,'*': 7,'+': 8};
	return $A($A(game.split("|")).map(
		function(row) {
			return $A(
				row.split('').map(function(p) {return prevod[p];})
			);
		}
	));
}
function zacatek() {
	var hra =parse_game(Game);
	var rows = Mrow = hra.length - 1;
	var cols = Mcol = hra[0].length - 1;
	var plyid;
	var tabule = [];
	for (var r = 0; r <= rows; r++) {
		tabule.push([]);
		for (var c = 0; c <= cols; c++) {
			tabule[r][c] = hra[r][c] == 9 ? okoli(hra,rows,cols,r,c) : 'P'+hra[r][c].toString();
		}
	}
	var ht= '';
	var r = -1, c= -1;
	tabule.each(function(row){
		ht = ht + '<div>';
		r++;
		c=-1;
		row.each(function(col) {
			c++;
			var id="R"+r.toString() +"C" + c.toString();
			var x = hra[r][c];
			if ( (/^[85]$/).test(x) ) {plyid = id}
			ht = ht + '<img r="' + r +'" c="' + c +'" stav="' + x + '" id="' + id +'" src="./img/'+ col +'.gif"/>'
			});
		ht = ht + '</div>'
		});
	$('sokob').update(ht);
	Player = $(plyid);
	TahCount = 0;
	PushCount = 0;

}
function okoli (hra,rows,cols,r,c) {
	var str = '';
	var code = '';
	var k = 0;
	for(var i = r-1; i <= r+1; i++) {
		for (var j = c-1; j <= c+1; j++) {
			k++;
			str = str + ( (i < 0 || i > rows || j < 0 || j > cols) ? '0' : hra[i][j] == 9 ? '9' : '0');
			code = code + ( (i < 0 || i > rows || j < 0 || j > cols) ? '' : hra[i][j] == 9 ? k.toString() : '');
		}
	}
	str = okoli_to_pict(str) || code;
	return str;
}
function okoli_to_pict(str) {
if ((/(?:90(?:0090(?:00[09]|90[09])|9090(?:00[09]|90[09]))|00(?:9090(?:00[09]|90[09])|0090(?:90[09]|009)))/).test(str)) { return '5'; }
if ((/(?:99(?:0090(?:00[09]|90[09])|9090(?:00[09]|90[09]))|09(?:9090(?:00[09]|90[09])|0090(?:90[09]|009)))/).test(str)) { return '25'; }
if ((/(?:00(?:9990(?:00[09]|90[09])|0990(?:90[09]|009))|90(?:0990(?:00[09]|90[09])|9990(?:90[09]|009)))/).test(str)) { return '45'; }
if ((/(?:90(?:0099(?:00[09]|90[09])|9099(?:00[09]|90[09]))|00(?:9099(?:00[09]|90[09])|0099(?:90[09]|009)))/).test(str)) { return '56'; }
if ((/(?:00(?:9090(?:09[09]|99[09])|0090(?:99[09]|099))|90(?:0090(?:09[09]|99[09])|9090(?:99[09]|090)))/).test(str)) { return '58'; }
if ((/09(?:9990(?:00[09]|90[09])|0990(?:009|900))/).test(str)) { return '245'; }
if ((/(?:990099(?:00[09]|90[09])|090099(?:90[09]|009))/).test(str)) { return '256'; }
if ((/(?:99(?:0090(?:09[09]|99[09])|9090(?:09[09]|99[09]))|09(?:9090(?:09[09]|99[09])|0090(?:99[09]|099)))/).test(str)) { return '258'; }
if ((/(?:90(?:0999(?:00[09]|90[09])|9999(?:00[09]|90[09]))|00(?:9999(?:00[09]|90[09])|0999(?:90[09]|009)))/).test(str)) { return '456'; }
if ((/(?:00(?:999009[09]|0990099)|90(?:099009[09]|9990099))/).test(str)) { return '458'; }
if ((/(?:00(?:9099[09]|00999)|90[09]099[09])90/).test(str)) { return '568'; }
if ((/99(?:9990(?:00[09]|90[09])|0990(?:90[09]|009))/).test(str)) { return '1245'; }
if ((/(?:999099(?:00[09]|90[09])|099099(?:90[09]|009))/).test(str)) { return '2356'; }
if ((/090999(?:90[09]|009)/).test(str)) { return '2456'; }
if ((/09(?:999009[09]|0990099)/).test(str)) { return '2458'; }
if ((/(?:990099[09]|0900999)90/).test(str)) { return '2568'; }
if ((/(?:90[09]|009)999090/).test(str)) { return '4568'; }
if ((/(?:90(?:099099[09]|999099[09])|00(?:999099[09]|0990999))/).test(str)) { return '4578'; }
if ((/(?:00(?:9099[09]|00999)|90[09]099[09])99/).test(str)) { return '5689'; }
if ((/990999(?:90[09]|009)/).test(str)) { return '12456'; }
if ((/99(?:999009[09]|0990099)/).test(str)) { return '12458'; }
if ((/099999(?:90[09]|009)/).test(str)) { return '23456'; }
if ((/(?:999099[09]|0990999)90/).test(str)) { return '23568'; }
if ((/09(?:999099[09]|0990999)/).test(str)) { return '24578'; }
if ((/(?:990099[09]|0900999)99/).test(str)) { return '25689'; }
if ((/(?:90[09]|009)999990/).test(str)) { return '45678'; }
if ((/(?:90[09]|009)999099/).test(str)) { return '45689'; }
if ((/999999(?:90[09]|009)/).test(str)) { return '123456'; }
if ((/99(?:999099[09]|0990999)/).test(str)) { return '124578'; }
if ((/(?:999099[09]|0990999)99/).test(str)) { return '235689'; }
if ((/(?:90[09]|009)999999/).test(str)) { return '456789'; }
return '';
}

function move(druh, smer) {
	var ele, upele,stav;
	switch (smer) {
	case 'up':
		ele = Player.eleup();
		break;
	case 'down':
		ele = Player.eledown();
		break;
	case 'left':
		ele = Player.eleleft();
		break;
	case 'right':
		ele = Player.eleright();
		break;
	}
	if (druh == 2) {
		switch (smer) {
		case 'up':
			upele = ele.eleup();
			break;
		case 'down':
			upele = ele.eledown();
			break;
		case 'left':
			upele = ele.eleleft();
			break;
		case 'right':
			upele = ele.eleright();
			break;
		}
	}
	stav = Player.stav = Number(Player.stav)-5;
	Player.src = stav.toSrc();
	stav = Number(ele.stav)+ 5;
	if(druh==2) {stav -= 4;}
	ele.stav = stav;
	ele.src = stav.toSrc();
	if(druh==2) {
		stav = Number(upele.stav)+4;
		upele.stav = stav;
		upele.src = stav.toSrc();
	}
	Player = ele;
}
function tahni(e) {
	e = e || window.event;
	var x = e.keyCode;
	var d,smer;
	switch (x) {
                case 27:
                       $('sokob').update();
                       zacatek();
                       return;

		case 38:
		case 87:
			d = Player.canup();
			if(d === 0) {return;}
			smer = 'up';
			break;
		case 40:
		case 83:
			d = Player.candown();
			if(d === 0) {return;}
			smer = 'down';
			break;
		case 39:
		case 68:
			d = Player.canright();
			if( d === 0) {return;}
			smer = 'right';
			break;
		case 37:
		case 65:
			d= Player.canleft();
			if(d == 0 ){return;}
			smer = 'left';
			break;
		default: return;
		}


	move(d,smer);
	TahCount++;
	if(d == 2) {PushCount++;}
	$('jinydiv').value = TahCount.toString()+ ' - ' + PushCount.toString();
}

var xp;
var runner;
function onemove() {
    var x = xp.shift();
    $('solution').value = xp.join('');
    if(!x) {
        window.clearInterval(runner);
        return;
    }
    automat(x);
}

function showres() {
    $('jinydiv').value = '';
    var sol = $('solution').value;
    if(!sol ) {return;}
    xp = sol.split('');
    runner = window.setInterval(onemove, 500);
}

function automat(x) {
	var d,smer;
        x = x.toLowerCase();
	switch (x) {
		case 'u':
			d = Player.canup();
			if(d === 0) {return;}
			smer = 'up';
			break;
		case 'd':
			d = Player.candown();
			if(d === 0) {return;}
			smer = 'down';
			break;
		case 'r':
			d = Player.canright();
			if( d === 0) {return;}
			smer = 'right';
			break;
		case 'l':
			d= Player.canleft();
			if(d == 0 ){return;}
			smer = 'left';
			break;
		}
	move(d,smer);
	TahCount++;
	if(d == 2) {PushCount++;}
	$('jinydiv').value = TahCount.toString()+ ' - ' + PushCount.toString();
}
